import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        registerNotification()
        //实例化Window
        self.window = UIWindow()
        window?.backgroundColor = UIColor.redColor()
        //可视化
        window?.makeKeyAndVisible()
        //设置根视图控制器
        window?.rootViewController = defaultRootViewController()
        setThemeColor()

        return true
    }
    @objc private func AppRootViewController(n:NSNotification) {
        print(n,n.object)
        if n.object != nil {
            window?.rootViewController = MainController()
        }else{
            window?.rootViewController = WelcomeViewController()
        }
    }
    
    private func registerNotification() {
        // object 观测哪一个对象发出的通知  如果为 nil 默认是所有对象

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "AppRootViewController:", name: KChoseRootVC, object: nil)
    }
    private func defaultRootViewController() ->UIViewController {
        //判断用户是否登录
        if UserAccountViewModel.sharedUserAccountViewModel.userLogin {

            return WelcomeViewController()
        }
            return MainController()
    }
    //MARK:在开始的时候设置渲染颜色
    private func setThemeColor() {
        //控件实例化之前就需要设置渲染颜色
        UINavigationBar.appearance().tintColor = themeColor
        UITabBar.appearance().tintColor = themeColor
    }
}

